<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">文章资讯信息</div>
                <div class="layui-card-body" pad15>
                    <div class="layui-form layui-form-pane" lay-filter="{$lay_table}">
                        <input type="hidden" name="id" value="{:array_get($info,'id')}">
                        <div class="layui-form-item">
                            <label class="layui-form-label"><span style="color:#f90c05;">*</span>分类</label>
                            <div class="layui-input-block">
                                <select name="cate_id">
                                    {volist name="cate_list" id="vo"}
                                    <option value="{$vo.id}" {:array_get($info,'cate_id')==$vo.id ? 'selected' : '' }>{$vo.title}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label"><span style="color:#f90c05;">*</span>标题</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" value="{:array_get($info,'title')}" lay-verify="required"
                                       placeholder="请输入标题" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">头图</label>
                            <div class="layui-input-block">
                                <div class="layui-upload">
                                    <button type="button" class="layui-btn attachment-upload-images"
                                            data-input="header_image" data-amount="1">上传图片
                                    </button>
                                    <div class="layui-inline layui-word-aux"> 最佳尺寸：110px*85px</div>
                                    <div class="layui-upload-list echo-attachment-image-list" id="header_image">
                                        {notempty name="info.header_image"}
                                        <div>
                                            <input type="hidden" name="header_image" value="{:array_get($info,'header_image')}">
                                            <img src="{:array_get($info,'header_image')}">
                                        </div>
                                        {/notempty}
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">作者</label>
                            <div class="layui-input-block">
                                <input type="text" name="author" value="{:array_get($info,'author')}" placeholder="请输入作者"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">概要</label>
                            <div class="layui-input-block">
                                <textarea name="summary" placeholder="请输入概要" class="layui-textarea">{:array_get($info,'summary')}</textarea>
                            </div>
                        </div>
                        <div class="layui-form-item" pane>
                            <label class="layui-form-label"><span style="color:#f90c05;">*</span>类型</label>
                            <div class="layui-input-block">
                                {volist name="type_list" id="v"}
                                <input type="radio" name="type" lay-filter="type" value="{$key}" title="{$v}" {eq name=":array_get($info,'type',1)" value="$key" }checked{/eq}>
                                {/volist}
                            </div>
                        </div>
                        <div class="layui-form-item" id="form-content">
                            <label class="layui-form-label"><span style="color:#f90c05;">*</span>详情</label>
                            <div class="layui-input-block">
                                <script id="editor" type="text/plain">{:array_get($info,'content')}</script>
                            </div>
                        </div>
                        <div class="layui-form-item" id="form-url">
                            <label class="layui-form-label"><span style="color:#f90c05;">*</span>跳转链接</label>
                            <div class="layui-input-block">
                                <input type="text" name="url" value="{:array_get($info,'url')}" placeholder="请输入跳转链接"
                                       autocomplete="off" class="layui-input">
                                <div class="layui-form-mid layui-word-aux">
                                    <b>链接：</b>url链接，以“http://”或“https://”开头。
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label"><span style="color:#f90c05;">*</span>排序</label>
                            <div class="layui-input-block">
                                <input type="text" name="priority" value="{:array_get($info,'priority',255)}"
                                       lay-verify="required|number" placeholder="请输入排序"
                                       autocomplete="off" class="layui-input">
                                <div class="layui-form-mid layui-word-aux">倒序，值越大越靠前</div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label"><span style="color:#f90c05;">*</span>状态</label>
                            <div class="layui-input-block">
                                {volist name="status_list" id="v"}
                                <input type="radio" name="status" value="{$key}" title="{$v}" {eq name=":array_get($info,'status',1)" value="$key" }checked{/eq}>
                                {/volist}
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label"><span style="color:#f90c05;">*</span>浏览量</label>
                            <div class="layui-input-block">
                                <input type="text" name="volume" value="{:array_get($info,'volume',0)}"
                                       lay-verify="required|number" placeholder="请输入浏览量"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <input type="button" lay-submit lay-filter="{$lay_btn}" value="确认提交" class="layui-btn">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/ueditor" /}
<script>
    layui.use(['index', 'form', 'layedit', 'upload'], function () {
        UE.getEditor('editor');
        const $ = layui.$;
        const admin = layui.admin;
        const layedit = layui.layedit;
        const form = layui.form;
        const upload = layui.upload;
        form.render();

        const content = layedit.build('content', {
            uploadImage: { url: "{:url('upload/image')}", type: 'post' }
        });

        form.verify({
            editcontent: function (value) {
                layedit.sync(content);
            }
        });

        function typeChange(value) {
            if (value == 1) {
                $("#form-content").show();
                $("#form-url").hide();
            } else {
                $("#form-content").hide();
                $("#form-url").show();
            }
            ;
        }
        typeChange("{:array_get($info,'type',1)}");
        form.on('radio(type)', function (data) {
            typeChange(data.value);
        });

        form.on('submit({$lay_btn})', function (obj) {
            const index = parent.layer.getFrameIndex(window.name);
            obj.field.content = UE.getEditor('editor').getContent();
            admin.req({
                url: "{:url('article/editArticle')}",
                type: 'post',
                data: obj.field,
                done: function (res) {
                    layer.msg("提交成功", {
                        icon: 1
                    });
                    parent.layui.table.reload('lay-article-index-table'); //重载表格
                    parent.layer.close(index);
                }
            });
        });


        $('.echo-attachment-image-list').on('click', '.attachment-del', function () {
            $(this).parent().remove();
        });

        let upload_input = '';
        let upload_amount = '';
        upload.render({
            elem: '.attachment-upload-images',
            url: "{:url('upload/image')}",
            accept: 'images',
            exits: 'jpg|png|jpeg',
            acceptMime: 'image/*',
            size: 2048,
            number: 1,
            method: 'post',
            before: function (obj) {
                upload_input = $(this.item).data('input');
                upload_amount = $(this.item).data('amount');
                layer.load();
            },
            done: function (res, index, upload) {
                layer.closeAll('loading');
                let html = "";
                if (upload_amount == 1) {
                    html += '<div> <input type="hidden" name="' + upload_input + '" value="' + res.data.src + '"> ';
                    html += '<img src="' + res.data.src + '"></div>';
                    $("#" + upload_input).html(html);
                } else {
                    html += '<div> <input type="hidden" name="' + upload_input + '[]" value="' + res.data.src + '"> ';
                    html += '<img src="' + res.data.src +
                        '"> <button type="button" class="attachment-del layui-btn layui-btn-primary layui-btn-xs layui-btn-fluid">删除</button></div>';
                    $("#" + upload_input).append(html);
                }
            },
            error: function (index, upload) {
                layer.closeAll('loading');
            }
        });

    });
</script>
